﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FuturamaKassa.Objects;
using FuturamaKassa.Database;
using FuturamaKassaWithGUI;
using FuturamaKassaWithGUI.Parser;
using FuturamaKassaWithGUI.Objects;

namespace FuturamaKassa.Commands.ReceiveCommands {
    class GetInventory : ReceiveCommand {

        delegate void AddRowCallback(List<Product> products);

        public override void Execute(List<byte> response) {

            //Remove control bits from the response
            response.RemoveRange(0, 10);
            response.RemoveRange(response.Count - 2, 2);

            //Create a list of the IDs and get data from the DataService
            List<string> idList = RFIDFunctions.SplitByNumber(Encoding.ASCII.GetString(response.ToArray()), 16);

            idList = RFIDFunctions.revertBitOrder(idList);

            DataService data = new DataService();
            string xmlString = data.getTags(idList);

            Logger.Instance.writeLog(xmlString);

            //Parse xmlString gotten from the DataService
            XMLParser parser = new XMLParser(xmlString);
            List<Product> products = parser.parse();

            AddRowCallback d = new AddRowCallback(Kassa.GetInstance().addRow);
            Kassa.GetInstance().Invoke(d, products);

           // Logger.Instance.writeLog("Command Get Inventory (6C21) executed");

            Inventory.GetInstance().Products = idList;
            Inventory.GetInstance().Wait = false;
        }
    }
}
